<?php
/**
 * 收藏和关注用户或者咨询
 */
use \Illuminate\Support\Facades\Event;
class CollectController extends CenterController {

    public function add()
    {
        if (Input::server ( 'REQUEST_METHOD' ) == 'POST') {
            $posts = Input::all();
            
            Validator::extend('duplicate', function ($attr, $value, $parameters) use ($posts) {
            	$value = false;
				$value = Collect::exists($posts['collect_type'], $posts['item_id']);
            	return !$value;
            } );

            $rules = array (
                'collect_type' => array('required', 'duplicate','regex:/^(1|2|3)$/i'),
            );
            $messages = array(
                'collect_type.required' => '请提交关注类型',
                'collect_type.regex' => '提交的关注类型格式不正确',
            	'collect_type.duplicate' => '您已经收藏了'
            );

            if (isset($posts['collect_type']) && $posts['collect_type'] == '1') {
            	$rules['item_id'] = 'required|exists:posts,id';
                $messages['item_id.required'] = '请提交post的id';
				$messages['item_id.exists'] = '这个post不存在';
            }
            if (isset($posts['collect_type']) && $posts['collect_type'] == '2') {
				$rules['item_id'] = 'required|exists:user,id';
                $messages['item_id.required'] = '请提交用户的id';
				$messages['item_id.exists'] = '这个用户不存在';
            }
            if (isset($posts['collect_type']) && $posts['collect_type'] == '3') {
            	$rules['item_id'] = 'required|exists:articles,id';
            	$messages['item_id.required'] = '请提交文章的id';
            	$messages['item_id.exists'] = '这个文章不存在';
            }

            $validator = Validator::make ( $posts, $rules, $messages );
            if ($validator->fails()) {
                // the given data did not pass validation
                return $this->makeView(
                    'errors.400', 
                    $validator->errors(),
                    400
                );
            }

			$collect_type = $posts['collect_type'];
			$item_id = $posts['item_id'];
			try {

                $user = null;
				switch($collect_type)
				{
					case Collect::TYPE_POST:
						$post = Post::find($item_id);
						if(! Collect::collected_post($post))
						{
							Collect::collect_post($post);
							$user = User::find($post->creator_id);
							$collect_count = $post_extra = $post->postExtra()->first()->collects_count;
						}
						break;
					case Collect::TYPE_USER:
						$user = User::find($item_id);
						if(! Collect::collected_user($user))
						{
							$collect = Collect::collect_user($user);
							$collect_count = $user->collects_count;
						}
						break;
					case Collect::TYPE_ARTICLE:
						$article = Article::find($item_id);
						if(! Collect::collected_article($article))
						{
							$collect = Collect::collect_article($article);
							$user = User::find($article->creator_id);
							$collect_count = $article->collects_count;
						}
						break;
				}

                if ($user) {
				    UserScoresHistory::collect($user);
                }
			} catch(Exception $ex) {
                Log::error($ex);
                return $this->makeView(
                    'errors.503', 
                    '收藏失败，请稍后再试。',
                    503
                );
			}
        }
		Event::fire('add_collect');
        return $this->makeView(
            'general.201', 
            $collect_count,
        		201
        );
    }

    public function delete() 
    {
        if (Input::server ( 'REQUEST_METHOD' ) == 'POST') {
            $posts = Input::all();

            $collect_type = $posts['collect_type'];

            Validator::extend ( 'exists_collect', function ($attr, $value, $parameters) use ($collect_type) {
                return (bool)Collect::whereRaw(
                    'collect_type = ? and item_id = ? and creator_id = ?', 
                    array(
                        $collect_type,
                        Input::get('item_id'),
                        $this->user->id
                    )
                )->count();
            } );

            $rules = array (
                'collect_type' => array('required','regex:/^(1|2|3)$/i'),
                'item_id' => 'required|exists_collect',
            );
            $messages = array(
                'collect_type.required' => '请提交收藏类型',
                'collect_type.regex' => '提交的关注类型格式不正确',
                'item_id.required' => '提交的关注的id',
                'item_id.exists_collect' => '这个关注不存在',
            );

            $validator = Validator::make ( $posts, $rules, $messages );
            if ($validator->fails()) {
                // the given data did not pass validation
                return $this->makeView(
                    'errors.400', 
                    $validator->errors()
                );
            }

            $item_id = $posts['item_id'];
            try {
                $collect = Collect::whereRaw(
                    'collect_type = ? and item_id = ? and creator_id = ?', 
                    array(
                        $collect_type,
                        Input::get('item_id'),
                        $this->user->id
                    )
                ); 
                $collect_item = $collect->first();
                switch($collect_item->collect_type)
                {
                	case Collect::TYPE_USER:
						$resutl = Collect::delete_collect_user($collect_item);
						break;
					case Collect::TYPE_POST:
						$resutl = Collect::delete_collect_post($collect_item);
						break;
                	case Collect::TYPE_ARTICLE:
                		$resutl = Collect::delete_collect_article($collect_item);
                		break;
                }
                
            } catch(Exception $ex) {
                Log::error($ex);
                return $this->makeView(
                    'errors.503', 
                    '请求失败，请稍后再试。',
                    503
                );
            }
        }

        return $this->makeView(
            'general.200', 
            '成功取消收藏'
        );
    }
}


